package drawingtools;

import gui.DrawingPanel;

import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.util.Vector;

import program.DatabaseConnection;

import utils.Point;
import diagramobjects.DiagramObjects;
import diagramobjects.ArrowsTextLine;

public class SelectionTool extends DrawingTool { 
	
	private DiagramObjects DiagramObjs;
	//private Line tempLine;
	private DrawingPanel DrawPanel;
	private Integer FirstX;
	private Integer FirstY;
	
	public SelectionTool(DiagramObjects go, DrawingPanel dp)
	{
		DiagramObjs = go;
		DrawPanel = dp;
	}
	
	@Override
	public void mouseClicked(MouseEvent arg0) 
	{
	
		
	}




	public void mousePressed(MouseEvent arg0) {
		super.mousePressed(arg0);
		System.out.println("mouseClicked, clickcount:" + ClickCount);
		FirstX = arg0.getX();
		FirstY = arg0.getY();
		DrawPanel.SetSelectionRect(FirstX, FirstY, FirstX, FirstY);


	}
	
	

	@Override
	public void mouseReleased(MouseEvent arg0) {
		super.mouseReleased(arg0);
		DrawPanel.ResetSelectionRect();
		boolean smthSelected = false;
		
		//System.out.println("SelectionTool->mouseClicked()");
		System.out.println("SelectionTool -> MR)");
		//SelectionTool
		for (int i = 0; i < DiagramObjs.GetSize(); ++i)
		{

			Integer Left, Right, Top, Bottom;
			Integer additional = 2;
			if (FirstX < arg0.getX())
			{
				Left = FirstX;
				Right = arg0.getX();
			}
			else if (FirstX > arg0.getX())
			{
				 Right = FirstX;
				 Left = arg0.getX();
			}
			else 
			{
				 Right = FirstX+additional;
				 Left = FirstX-additional;
			}

			
			if (FirstY < arg0.getY())
			{
				Top = FirstY;
				Bottom = arg0.getY();
			}
			else if (FirstY > arg0.getY())
			{
				Bottom = FirstY;
				Top= arg0.getY();
			}
			else 
			{
				 Top = FirstY - additional;
				 Bottom = FirstY + additional;
			}
			
				
			if (DiagramObjs.GetGeometricbjectFromPanel(i).Selection(Left, Right, Top, Bottom))
			{
				//DrawPanel.CleanAllSelections();
				
				DrawPanel.changeSelection(i);
				System.out.println("DrawPanel -> ChangeSelection(" + i + ")");
				if (smthSelected == false)
					smthSelected = true;
				
			}
				
		}
		if (smthSelected == false)
			DrawPanel.CleanAllSelections();
		DrawPanel.repaint();
	}

	@Override
	public void mouseDragged(MouseEvent e) {
		super.mouseDragged(e);
		
		DrawPanel.SetSelectionRect(FirstX, FirstY, e.getX() - FirstX, e.getY() - FirstY);
		DrawPanel.repaint();
		System.out.println("SelectionTool -> MD)");
	}



	

}
